<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/6/19.
 */

namespace App\Http\Controllers\System;

use App\Enum\StatusEnum;
use App\Http\Controllers\Controller;
use App\Http\Request\System\ChannelModelRequest;
use App\Models\ChannelModel;
use App\Service\ChannelModelService;
use App\Utils\ResultsVo;

class ChannelModelController extends Controller
{
    protected $channelModelService;

    public function __construct(ChannelModelService $channelModelService)
    {
        parent::__construct();
        $this->channelModelService = $channelModelService;
    }

    public function index(): \Illuminate\Http\JsonResponse
    {
        $data = $this->channelModelService->lists();

        return ResultsVo::success($data);
    }

    public function lists(): \Illuminate\Http\JsonResponse
    {
        $results = ChannelModel::query()
            ->select(['id', 'title'])
            ->where('status', StatusEnum::ENABLE)
            ->orderBy('weight', 'desc')->get()->toArray();

        $data = [];
        foreach ($results as $result) {
            $data[] = [
                'value' => $result['id'],
                'label' => $result['title'],
            ];
        }

        return ResultsVo::success($data);
    }

    public function store(ChannelModelRequest $request): \Illuminate\Http\JsonResponse
    {
        $this->channelModelService->store($request->all());

        return ResultsVo::success();
    }

    public function edit(ChannelModelRequest $request, $id): \Illuminate\Http\JsonResponse
    {
        $this->channelModelService->edit($request->all(), $id);

        return ResultsVo::success();
    }

    public function del($id): \Illuminate\Http\JsonResponse
    {
        $this->channelModelService->delTable($id);

        return ResultsVo::success();
    }
}
